package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.activity.home.HomeActivity;
import com.mz.ozy.adapter.RoleAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.RoleListBean;
import com.mz.ozy.bean.SwitchRoleBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.SwitchRolePresenter;
import com.mz.ozy.mvp.view.SwitchRoleContract;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.dialog.AddRoleWindow;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SwitchRoleActivity extends BaseActivity<SwitchRoleContract.View, SwitchRolePresenter> implements SwitchRoleContract.View {


    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private RoleAdapter mHomeAdapter;
    private String selectHeaderUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_role);
        ButterKnife.bind(this);
        initTitle();
        tvTitle.setVisibility(View.GONE);
        tvCenterTitle.setText(R.string.str_account_title);

        initView();
        mPresenter.getRoleLevelList(this);
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mHomeAdapter = new RoleAdapter(null);
        recyclerview.setAdapter(mHomeAdapter);
        mHomeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mHomeAdapter.setSelectIndex(position);
                final RoleListBean.RoleBean item = mHomeAdapter.getItem(position);
                selectHeaderUrl = item.getUseravatar();
                mPresenter.switchRoles(SwitchRoleActivity.this, item.getUid());
            }
        });
    }

    private AddRoleWindow mPayPopupWindow;


    public void showPaypwdPop() {
        if (mPayPopupWindow == null) {
            mPayPopupWindow = new AddRoleWindow(this, new AddRoleWindow.OnClickListener() {
                @Override
                public void onConfirm(String nickName, String inviteCode) {
                    mPresenter.addRole(SwitchRoleActivity.this, nickName, inviteCode);
                }
            });
        }
        mPayPopupWindow.showAtLocation(titleBar, Gravity.BOTTOM, 0, 0);


    }

    @Override
    public void getRoleListSuccess(RoleListBean roleListBean) {
        final List<RoleListBean.RoleBean> roleBeans = roleListBean.getkList();
        if (roleBeans != null && roleBeans.size() > 0) {
            for (RoleListBean.RoleBean bean : roleBeans) {
                if (1 == bean.getXzJs()) {
                    SPUtils.getInstance(this).putString(Constants.SPName.USERNAME, bean.getUsername());
                }
            }
            mHomeAdapter.setNewData(roleBeans);
        }
    }

    @Override
    public void addRoleSuccess(RoleListBean json) {
        mPresenter.getRoleLevelList(this);
    }

    @Override
    public void switchRolesSuccess(SwitchRoleBean bean) {
        ToastUtils.showToast(R.string.switch_success);
        final SwitchRoleBean.RoleMapBean roleMapBean = bean.getaMap();
        if (roleMapBean != null) {
            HomeActivity.grade = roleMapBean.getGrade();
            SPUtils.getInstance(this).putString(Constants.SPName.USERAVATAR, roleMapBean.getUseravatar());
            SPUtils.getInstance(this).putString(Constants.SPName.GAMECODE, roleMapBean.getCode());
            SPUtils.getInstance(this).putString(Constants.SPName.TOKEN_ID, roleMapBean.getT());
            Constants.tokenId = SPUtils.getInstance(this).getString(Constants.SPName.TOKEN_ID, "");
        }
        Intent intent = new Intent(this, SettingActivity.class);
        intent.putExtra("url", selectHeaderUrl);
        setResult(100, intent);
        finish();
    }
}
